<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="isShow"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="rowsList">
              <el-table-column label="序号" width="120" />
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success" @click="btnisShowPerm(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="delBtn(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                :total="total"
                :page-size="pageConfig.pagesize"
                layout="prev,pager,next"
                @current-change="currentChange"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="公司名称">
                <el-input
                  v-model="companyInfo.name"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  v-model="companyInfo.companyAddress"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  v-model="companyInfo.mailbox"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="companyInfo.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <el-dialog :title="formData.id?'编辑角色':'新增角色'" :visible="isShowDialog" @close="btnNo">
        <el-form ref="addRole" :rules="rules" :model="formData">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="formData.description" />
          </el-form-item>
        </el-form>
        <template #footer>
          <el-button type="primary" size="small" @click="btnOk">确认</el-button>
          <el-button size="small" @click="btnNo">取消</el-button>
        </template>
      </el-dialog>

      <el-dialog title="设置权限列表" :visible="isShowPerm" @close="btnCancel">
        <el-checkbox-group v-model="permIds">
          <el-tree :data="permsList" :props="{label:'name'}" default-expand-all>
            <template #default="{data}">
              <el-checkbox :label="data.id">
                {{ data.name }}
              </el-checkbox>
            </template>
          </el-tree>
        </el-checkbox-group>
        <template #footer>
          <el-button type="primary" @click="btnPermOk">确定</el-button>
          <el-button @click="btnCancel">取消</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getCompanyInfo, getRoleList, addRole, getRoleDetail, updateRole, delRole, assignPerm } from '@/api/setting'
import { getPermList } from '@/api/permission'
import { listTotreeData } from '@/utils/index'
export default {
  data() {
    return {
      roleId: '',
      permIds: [],
      permsList: [],
      isShowPerm: false,
      companyInfo: {},
      pageConfig: {
        page: 1,
        pagesize: 2
      },
      rowsList: [],
      total: 0,
      isShowDialog: false,
      formData: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: 'true', message: '该项目不能为空', trigger: 'blur' }
        ],
        description: [
          { required: 'true', message: '该项目不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  async created() {
    // 公司信息的数据
    const res = await getCompanyInfo(this.$store.getters.compantInfo)
    this.companyInfo = res
    // 角色管理的数据
    this.getCompanyInfo()
    this.permsList = listTotreeData(await getPermList(), '0')
    // console.log(153, this.permsList)
  },
  methods: {
    async btnPermOk() {
      await assignPerm({
        id: this.roleId,
        permIds: this.permIds
      })
      this.$message.success('更改成功')
      this.isShowPerm = false
    },
    btnCancel() {
      this.isShowPerm = false
    },
    async btnisShowPerm(roleId) {
      this.roleId = roleId
      const { permIds } = await getRoleDetail(roleId)
      this.permIds = permIds
      this.isShowPerm = true
    },
    async getCompanyInfo() {
      const { rows, total } = await getRoleList(this.pageConfig)
      // console.log(78, total)
      this.rowsList = rows
      this.total = total
    },
    currentChange(newPage) {
      console.log(94, newPage)
      this.pageConfig.page = newPage
      this.getCompanyInfo()
    },
    isShow() {
      this.isShowDialog = true
    },
    // 确定
    async btnOk() {
      try {
        await this.$refs.addRole.validate()
        // 有id就是编辑,没有id就是新增
        if (this.formData.id) {
          await updateRole(this.formData)
        } else {
          await addRole(this.formData)
        }
        this.isShowDialog = false
        this.$message.success('操作成功')
        this.getCompanyInfo()
      } catch (error) {
        console.log(error)
      }
    },
    // 取消和 x
    btnNo() {
      this.formData = {
        name: '',
        description: ''
      }
      this.$refs.addRole.resetFields()
      this.isShowDialog = false
    },
    // 编辑角色
    async editRole(id) {
      console.log(id)
      this.formData = await getRoleDetail(id)
      this.isShowDialog = true
    },
    // 删除
    async delBtn(id) {
      await this.$confirm('确定要删除吗')
      await delRole(id)
      this.$message.success('删除成功')
      if (this.rowsList.length === 1 && this.pageConfig.page > 1) {
        this.pageConfig.page -= 1
      }
      this.getCompanyInfo()
    }
  }
}
</script>

<style>
</style>
